package com.lxw.report.engine.dataModel.report;

import com.lxw.report.engine.enums.BIColumnTypeEnum;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 报表每一行的源数据
 */
public class ReportDataRow {
    private  Map<String, ReportDataCell> cells = new HashMap<>();
    //布局列key值
    private  StringBuilder layOutRowKeyBuilder = new StringBuilder("");
    //布局列key值
    public ReportDataRow() {
    }
    public ReportDataRow add( ReportDataCell cell) {
        this.cells.put(cell.getName(), cell);
        if (cell.getColumn().getBiColumnType() == BIColumnTypeEnum.LEFT_DIMENSION) {
            Object cellValue = cell.getValue();
            this.layOutRowKeyBuilder.append((cellValue == null) ? "" : StringUtils.replace(cellValue.toString(), ReportDataSet.PATH_SEPARATOR, "*"));
            this.layOutRowKeyBuilder.append("$");
        }
        return this;
    }
    public ReportDataRow addAll( List<ReportDataCell> cells) {
        cells.forEach(this::add);
        return this;
    }
    public Map<String, ReportDataCell> getCells() {
        return this.cells;
    }

    public ReportDataCell getCell( String name) {
        return this.cells.get(name);
    }

    public Object getCellValue( String name) {
         ReportDataCell cell = this.cells.get(name);
        return (cell == null) ? null : cell.getValue();
    }

    public String getLayOutRowKey() {
        return this.layOutRowKeyBuilder.toString();
    }
}
